import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt

# 霍夫直线检测
# 读取图像
img = cv.imread("image/rili.jpg")

# 进行Canny边缘检测
edges = cv.Canny(img, 50, 150)
plt.imshow(edges, cmap=plt.cm.gray)
plt.show()

# 在此基础上进行霍夫变换获取所有直线
lines = cv.HoughLines(edges, 0.8, np.pi/180, 150)

for line in lines:
    rho, theta = line[0]
    a = np.cos(theta)
    b = np.sin(theta)
    x0 = rho * a
    y0 = rho * b
    x1 = int(x0 + 1000 * (-b))
    y1 = int(y0 + 1000 * a)
    x2 = int(x0 - 1000 * (-b))
    y2 = int(y0 - 1000 * a)
    cv.line(img, (x1, y1), (x2, y2), (255, 255, 0))

plt.imshow(img[:, :, ::-1])
plt.show()